

'''
    需求:工具函数分离

'''

import  requests
import hashlib
import pickle
import re
import time
from server_info import register,login
from Util import *



#print(999,get_testcase_data(data_path))
test_data= 'data.txt'
log_path= 'log.log'




#获取唯一用户名
username= 'kkkk' + get_uniquenumber(unique_number_file_path)
#print(1,username)
#注册接口信息
register_url= register
register_http_method= 'post'
#请求数据和断言信息
register_data= '{"username":"%s","password":"12345678abc","email":"wxh@qq.com"}' %(username,)
register_assert_word= '{"code": "00"'
#执行请求和断言
start_time= time.time()
req= send_request(register_url,register_data,'post')
end_time= time.time()
elapse_time= round(abs(start_time-end_time),2)

print(11,req.text)
testresult= assert_result(req.text, register_assert_word)
write_log(logpath, register_url, 'post', register_data, req.text, elapse_time, testresult, register_assert_word)





#登录接口业务处理
login_url= login
login_http_method=  'post'
#执行请求和断言
login_data= '{"username":"%s","password":"%s","email":"wxh@qq.com"}' %(username, md5('12345678abc'),)
login_assert_word= '{"code": "00"'
login_re_assert_word= r'token": "\w{32}"'
#执行请求和断言
start_time= time.time()
req= send_request(login_url,login_data,login_http_method)
end_time= time.time()
elapse_time= round(end_time-start_time,2)
test_result1= assert_result(req.text,login_assert_word)
test_result2= assert_result(req.text,login_re_assert_word)
if test_result1==test_result2:test_result='成功'
else:test_result='失败'
write_log(logpath, login_url, login_http_method, login_data, req.text, elapse_time, test_result, login_re_assert_word + ';' + login_assert_word)
#测试结果输出
output_result(total_test_case, success_test_case, fail_test_case)









testcase_path= 'data.txt'
log_path= 'log.log'
username= 'kkk'+get_uniquenumber('unique.txt')
for line in get_testcase_data(testcase_path):
    if re.search(r'\$\{(\w+)\}',line):
        var_name= re.search(r'\$\{(\w+)\}', line).group(1)
        var_value=eval(var_name)
        line= re.sub(r'\$\{(\w+)\}',var_value,line)
        #print(0,line)
    if re.search(r"\$\{md5\('\w+'\)\}", line):
        #print(2,line)
        md5_data = re.search(r"\$\{(md5\('\w+'\))\}",line).group(1)
        #print(1,md5_data)
        md5_data=eval(md5_data)
        #print(1,md5_data)
        line= re.sub(r"\$\{(md5\('\w+'\))\}",md5_data,line)
        print(3, line)
    print(1,line)
    url,test_data,http_method,assert_word= line.split('||')
    url=eval(url)
    print(1,url,test_data,http_method,assert_word)
    start_time= time.time()
    req= send_request(url,test_data,http_method)
    end_time= time.time()
    print(9,req,req.text)
    break
